package t10086.WorkThread;
import t10086.WorkThread.Shared.Client.Client;
import t10086.WorkThread.Shared.ClientMap;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;


public class ClientHandler implements Runnable {
    private final Socket socket;
    public ClientHandler(Socket socket) {
       this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InetSocketAddress socketAddress= (InetSocketAddress) socket.getRemoteSocketAddress();
            Client client =  new Client(socketAddress,socket);
            ClientMap.addClient(socketAddress, client);
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            ThreadPoolManager.submitTask(new Listener(socketAddress,ois));
            ThreadPoolManager.submitTask(new Sender(socketAddress,client.getSenderQueue(),oos));
        } catch (IOException e) {
             System.out.println(e.getMessage());
        }
    }
}